package com.example.sportrecord.SQL;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class SQLOperation {
    private SqlBatase sqlBatase;

    final static String sql_insert="insert into sport2(date,startTime,stopTime,All_useTime,explain1) values(?,?,?,?,?)";
//    final static String sql_find="SELECT *FROM sport2";
    ArrayList<User> list=new ArrayList<>();
    public SQLOperation(Context context) {
       this.sqlBatase=new SqlBatase(context);
    }
    //对数据库进行修改操作


    //插入数据
    public void insert(User user){
        SQLiteDatabase db=sqlBatase.getReadableDatabase();
        db.execSQL(sql_insert,new String[]{user.getDate(),user.getStartTime(),user.getStopTime(),user.getAll_usetime(),user.getExplain1()});
    }

    //删除数据
    public void deletOneData(String stopTime){
        String deleteSQL = "delete from sport2 where stopTime = ?";
        SQLiteDatabase db1 = sqlBatase.getReadableDatabase();
        db1.execSQL(deleteSQL,new String []{stopTime});
    }
    //查询数据库记录总条数
    public int searchTotalData(){
        String totalData = "select count(stopTime) from sport2";

        Cursor cursor=sqlBatase.getReadableDatabase().rawQuery(totalData,null);
        int totalNum=0;
        if(cursor.moveToNext()){
            totalNum = cursor.getInt(0);
        }
        return totalNum;
    }



    //获取数据库的每一条的具体信息
    public ArrayList<User> initData(){
        String sqlSelect = "select date,startTime,stopTime,All_useTime,explain1 from sport2 ";
        Cursor cursor = sqlBatase.getReadableDatabase().rawQuery(sqlSelect,new String[]{});
        while(cursor.moveToNext()){
            String date = cursor.getString(cursor.getColumnIndex("date"));
            String startTime = cursor.getString(cursor.getColumnIndex("startTime"));
            String stopTime = cursor.getString(cursor.getColumnIndex("stopTime"));
            String All_useTime = cursor.getString(cursor.getColumnIndex("All_useTime"));
            String explain1 = cursor.getString(cursor.getColumnIndex("explain1"));
            User user = new User(date,startTime,stopTime,All_useTime,explain1);
            list.add(user);

        }
        return list;
    }

}
